//
//  YMRentDepositView.swift
//  RentFreeDemo
//
//  Created by youmy on 2018/11/27.
//  Copyright © 2018 youmy. All rights reserved.
//

import UIKit

public class YMRentDepositView: UIView {
    
    public var debug:Bool = false {
        didSet{
            if debug {
                backgroundColor = UIColor(randomColorAlpha: 0.5)
                nameLb.backgroundColor = UIColor(randomColorAlpha: 0.5)
                priceLb.backgroundColor = UIColor(randomColorAlpha: 0.5)
                desLb.backgroundColor = UIColor(randomColorAlpha: 0.5)
            }else{
                backgroundColor = .white
                nameLb.backgroundColor = .white
                priceLb.backgroundColor = .white
                desLb.backgroundColor = .white
            }
        }
    }
    
    public var name:String = ""{
        didSet{
            nameLb.text = name
        }
    }
    
    public var price:String = "" {
        didSet{
            priceLb.text = "￥\(price)"
        }
    }
    
    public var des:String = ""{
        didSet{
            
            if des.isEmpty {
                desLb.text = des
                desLb.height = 0
            }else{
                desLb.text = des
                desLb.height = des.getHeight(font: desLb.font, width: ym_screen_width - 20 * 2)
            }
            layoutIfNeeded()
        }
    }
    
    public var nameColor:UIColor = UIColor(valueRGB: 0x242424, alpha: 1) {
        didSet{
            nameLb.textColor = nameColor
        }
    }
    
    public var priceColor:UIColor = UIColor(valueRGB: 0xff4230, alpha: 1) {
        didSet{
            priceLb.textColor = priceColor
        }
    }
    
    public var desColor:UIColor = UIColor(valueRGB: 0x828282, alpha: 1) {
        didSet{
            desLb.textColor = priceColor
        }
    }
    
    lazy var nameLb:UILabel = {
        let label = UILabel()
        let space:CGFloat = 20
        label.x = space
        label.y = 20
        label.width = ym_screen_width/2.0 - space
        label.height = 14
        label.font = UIFont(name: PingFangSC_Medium, size: 14)
        label.textColor = UIColor(valueRGB: 0x242424, alpha: 1)
        return label
    }()
    
    lazy var priceLb:UILabel = {
        let label = UILabel()
        let space:CGFloat = 20
        label.x = nameLb.frame.maxX
        label.y = 20
        label.width = ym_screen_width - nameLb.frame.maxX - space
        label.height = 14
        label.font = UIFont(name: PingFangSC_Medium, size: 14)
        label.textColor = UIColor(valueRGB: 0xff4230, alpha: 1)
        label.textAlignment = .right
        return label
    }()
    
    var desLb:UILabel!

    public override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        
        addSubview(nameLb)
        addSubview(priceLb)
        
        desLb = UILabel(frame: CGRect(x: 20, y: nameLb.frame.maxY + 8, width: ym_screen_width - 20 * 2, height: 11))
        desLb.text = ""
        desLb.font = UIFont(name: PingFangSC_Regular, size: 11)
        desLb.textColor = UIColor(valueRGB: 0x828282, alpha: 1)
        desLb.textAlignment = .justified
        desLb.numberOfLines = 0
        addSubview(desLb)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override public func layoutSubviews() {
        super.layoutSubviews()
        
        height = desLb.frame.maxY + (des.isEmpty ? 12 : 20)
    }
}
